$(function() {

    var page = 1;

    var pageSize = 5;

    var categorytotal = 0;

    $.ajax({
        url: '/category/querySecondCategoryPaging',
        type: 'get',
        data: {
            page: page,
            pageSize: pageSize,
        },
        success: function(res) {
            // console.log(res);

            categorytotal = Math.ceil(res.total / pageSize);

            var html = template('secondTpl', res);
            $('.table').html(html);

        }
    });



    $('#prevBtn').on('click', function() {

        page--;
        if (page < 1) {
            page = 1;
            alert('给不起客官，到顶了');
            return;
        }
        $.ajax({
            url: '/category/querySecondCategoryPaging',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize,
            },
            success: function(res) {
                var html = template('secondTpl', res);
                $('.table').html(html);

            }
        });

    });



    $('#nextBtn').on('click', function() {

        page++;
        if (page > categorytotal) {
            page = categorytotal;
            alert('给不起客官，给不了你跟多了');
            return;
        }
        $.ajax({
            url: '/category/querySecondCategoryPaging',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize,
            },
            success: function(res) {
                var html = template('secondTpl', res);
                $('.table').html(html);

            }
        });
    });



    // 二级分类添加

    $.ajax({
        url: '/category/queryTopCategoryPaging',
        type: 'get',
        data: {
            page: 1,
            pageSize: 50,
        },
        success: function(res) {

            var html = template('catesecondTpl', res);
            $('#list').html(html);
        }
    });

    // 保存图片地址
    var pic = '';

    // 上传图片
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function(e, data) {
            console.log(data.result.picAddr);
            $('#pic').attr('src', data.result.picAddr);

            pic = data.result.picAddr;
        }
    });

    $('#cateBtn').on('click', function() {

        var categoryId = $.trim($("[name='categoryId']").val());

        var brandName = $.trim($("[name='brandName']").val());

        if (!categoryId) {
            alert('请选择商品分类');
            return;
        }
        if (!brandName) {
            alert('请选择商品名称');
            return;
        }

        $.ajax({
            url: '/category/addSecondCategory',
            type: 'post',
            data: {
                brandName: brandName,
                categoryId: categoryId,
                brandLogo: pic,
                hot: 0

            },
            success: function(res) {
                // console.log(res);

                if (res.success) {
                    location.reload();
                }

            }
        });

    });

});